// @Author maotao
// Created by tao on 24-9-17.
// @Desc 制定规则

#include <Bird.h>
#include <Block.h>
#include <main.h>
#include <Game.h>
#include <oled.h>
#include <stdio.h>

unsigned int score = 0;
unsigned int scoreScale = 1;
char scoreText[SCORE_TEXT_SIZE_DEFAULT];

static Bool _isNotPassWall(const Bird *bird, const Block *block) {
    int y1 = block->gap_y, y2 = y1 + block->gap_size;
    int x1 = bird->x, x2 = x1 + bird->w, w1 = block->x, w2 = w1 + block->w;
    if ((w1 < x1 && x1 < w2) || (w1 < x2 && x2 < w2)) {
        if (y1 <= bird->y && bird->y + bird->h <= y2) {
            return false;
        }
        return true;
    }

    return false;
}

Bool isDead(const Bird *bird, const Block *blocks[], const int blocksSize) {
    for (int i = 0; i < blocksSize; i++) {
        if (_isNotPassWall(bird, blocks[i]))
            return true;
    }
    if (bird->y >= 63 || bird->y <= 0)
        return true;
    return false;
}

void autoScore(const Bird *bird, const Block *blocks[], const int blocksSize) {
    int cnt = 0;
    for (int i = 0; i < blocksSize; i++) {
        if (blocks[i]->x + blocks[i]->w == bird->x - 2)
            cnt++;
    }

    score += cnt * scoreScale;
}

void OLED_DrawGameOver() {
    OLED_PrintASCIIString(30, 30, "GAME OVER!", &afont16x8, OLED_COLOR_NORMAL);
}

void OLED_DrawScoreText() {
    sprintf(scoreText, "score: %d", score);
    OLED_PrintASCIIString(SCORE_TEXT_X_INIT, SCORE_TEXT_Y_INIT, scoreText, &afont8x6, OLED_COLOR_NORMAL);
}

